package com.tuanzi.loan.business.validation.validator.family;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.validation.annotation.family.ProductInfoBusinessValidation;
import com.tuanzi.loan.core.exception.ValidateException;

public class ProductInfoBusinessValidator implements ConstraintValidator<ProductInfoBusinessValidation, ProductInfo> {

	@Override
	public void initialize(ProductInfoBusinessValidation constraintAnnotation) {
	}

	@Override
	public boolean isValid(ProductInfo productInfo, ConstraintValidatorContext context) {

		if (productInfo.getDefaultAmount() != null && (productInfo.getDefaultAmount() > productInfo.getMaxLimit() || productInfo.getDefaultAmount() < productInfo.getMinLimit())) {
			throw new ValidateException("默认借款金额不能大于或小于最高/最低借款额度");
		}

		if (productInfo.getDefaultTime() != null && (productInfo.getDefaultTime() > productInfo.getMaxTerm() || productInfo.getDefaultTime() < productInfo.getMinTerm())) {
			throw new ValidateException("默认借款期限不能大于或小于最高短/长低借期限");
		}

		return true;
	}

}
